#include"database.h"
#include"stack.h"
// 删除文件或目录
int rm_command(MYSQL* conn, const char* name, DirStack* stack, const char* user_name) {
    int p_id;
    if (get_top(stack, &p_id) != 0) {
        fprintf(stderr, "路径栈为空，无法获取父目录ID\n");
        return -1;
    }

    MYSQL_STMT* stmt = mysql_stmt_init(conn);
    if (!stmt) {
        fprintf(stderr, "mysql_stmt_init failed\n");
        return -1;
    }

    // 删除节点，条件：name, user_name, p_id
    const char* sql = "DELETE FROM nodes WHERE name=? AND user_name=? AND p_id=?";
    if (mysql_stmt_prepare(stmt, sql, strlen(sql)) != 0) {
        fprintf(stderr, "mysql_stmt_prepare failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));

    // 绑定参数
    bind[0].buffer_type = MYSQL_TYPE_STRING;
    bind[0].buffer = (void*)name;
    bind[0].buffer_length = strlen(name);

    bind[1].buffer_type = MYSQL_TYPE_STRING;
    bind[1].buffer = (void*)user_name;
    bind[1].buffer_length = strlen(user_name);

    bind[2].buffer_type = MYSQL_TYPE_LONG;
    bind[2].buffer = (void*)&p_id;

    if (mysql_stmt_bind_param(stmt, bind) != 0) {
        fprintf(stderr, "mysql_stmt_bind_param failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    if (mysql_stmt_execute(stmt) != 0) {
        fprintf(stderr, "mysql_stmt_execute failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    mysql_stmt_close(stmt);
    return 0;
}
